<template>
    <div>
        <pagetitle type="2"></pagetitle>
        <el-card class="box-card">
            <div class="teamTopTip">
                <el-icon color="#252B3A" size="16px"><Warning /></el-icon>
                <div class="tipbox">
                    <div class="tiptitle">温馨提示</div>
                    <div class="tipitem">
                        当前个人账户下您有 {{ pageData.list ? pageData.list.length : "0" }} 个团队名额，加入团队 已使用 2 个，创建团队 已使用 1 个，已冻结 0 个。
                        <el-button type="primary" size="small" link>申请升级</el-button>
                    </div>
                    <div class="tipitem">
                        如果您要加入已经注册成功的团队，个人实名认证 成功后，请点击
                        <el-button type="primary" size="small" link @click="topages('/team/myteams/jointeam')">加入团队</el-button>
                    </div>
                    <div class="tipitem">
                        如果您是企业法人或者法人授权人，个人实名认证 成功后，您可以 注册新的团队，请点击
                        <el-button type="primary" size="small" link @click="topages('/team/myteams/teammsg?type=1')">创建团队</el-button>
                    </div>
                </div>
            </div>
        </el-card>
        <div class="teamBox">
            <div class="teamtop">
                <div class="left">
                    <el-input class="searchInput" v-model="queryData.keyWords" placeholder="搜索团队名称" @input="getTeamList" suffix-icon="Search" />
                    <!-- <el-button type="primary" @click="getTeamList()" text bg>搜索</el-button> -->
                    <sxnfbtn @click="getTeamList()" color="#23b14d">搜索</sxnfbtn>
                </div>
                <div class="right">
                    <sxnfbtn @click="topages('/team/myteams/teammsg?type=1')" color="#23b14d">创建团队</sxnfbtn>
                    <sxnfbtn @click="topages('/team/myteams/jointeam')" color="#23b14d">加入团队</sxnfbtn>
                    <!-- <el-button type="primary" @click="topages('/team/myteams/teammsg?type=1')" text bg>创建团队</el-button> -->
                    <!-- <el-button type="primary" @click="topages('/team/myteams/jointeam')" text bg>加入团队</el-button> -->
                </div>
            </div>
            <div class="table">
                <el-table :data="pageData.list" style="width: 100%" border :fit="true" class="configurationtable">
                    <el-table-column label="序号" width="50">
                        <template #default="scope">
                            {{ scope.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间" width="100">
                        <template #default="scope">
                            {{ scope.row.createTime ? scope.row.createTime.split(" ")[0] : "" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="teamName" label="团队名称" width="200">
                        <template #default="scope">
                            {{ scope.row.teamName }}
                            <span style="color: red; font-weight: bold; font-size: 12px" v-if="scope.row.isMainTeam == 1">主</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="legalPersonName" label="法人" width="80"></el-table-column>
                    <el-table-column prop="type" label="组织机构类型" width="160"></el-table-column>
                    <el-table-column prop="classify" label="团队产业分类">
                        <template #default="scope">
                            {{ scope.row.classify.join("/") }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="address" label="团队注册地址">
                        <template #default="scope">
                            {{ scope.row.address ? scope.row.address.valueText : "" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="departmentName" label="所属部门" width="100">
                        <template #default="scope">
                            {{ scope.row.departmentName ? scope.row.departmentName : "未加入部门" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="positionName" label="岗位" width="100">
                        <template #default="scope">
                            {{ scope.row.positionName ? scope.row.positionName : "未分配岗位" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="invitationWay" label="加入方式" width="80">
                        <template #default="scope">
                            {{ scope.row.invitationWay == "0" ? "被动加入" : "主动加入" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="certified" label="认证状态" width="80">
                        <template #default="scope">
                            {{ scope.row.certified == 1 ? "认证中" : scope.row.certified == 2 ? "已认证" : "未认证" }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="180">
                        <template #default="scope">
                            <el-button type="primary" size="small" link @click="topages(`/team/myteams/teammsg?type=2&id=${scope.row.teamId}`)">详细</el-button>
                            <el-button type="primary" size="small" link @click="topages(`/team/myteams/teammsg?type=3&id=${scope.row.teamId}`)">编辑</el-button>
                            <el-button type="primary" size="small" link v-if="scope.row.isMainTeam == 0" @click="setCurrentMainTeam(scope.row)">设为主团队</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination :totalNum="pageData.total" @paginaClick="paginaClick"></pagination>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, computed } from "vue";
import { useRouter } from "vue-router";
import { teamList, setMainTeams, teamMsg } from "@/api/team";
import { ElMessage, ElMessageBox } from "element-plus";
import { useStore } from "vuex";
// import { Flag } from "@element-plus/icons-vue/dist/types";
const router = useRouter();
const store = useStore();
let pageData: any = ref({});
const queryData = reactive({
    pageNum: 1,
    pageSize: 10,
    keyWords: "",
    flag: "1",
});
const selectCompany = computed({
    get: () => {
        return store.getters.mainTeamId;
    },
    set: (val) => {
        console.log(val, "触发set方法");
        store.commit("selectTeam", val);
        teamMsg(val).then((res: any) => {
            store.commit("mainTeam", res.data);
        });
    },
});
// 获取团队列表
const getTeamList = () => {
    teamList(queryData).then((res) => {
        pageData.value = res.data;
        pageData.value.list.forEach((v: any) => {
            v.classify = [];
            if (v.address) {
                v.address = JSON.parse(v.address);
            }
            v.industrialClassificationList.forEach((i: any) => {
                v.classify.push(i.industrialName);
            });
        });
    });
    teamList({
        pageNum: 1,
        pageSize: 100,
        flag: "1",
    }).then((res) => {
        if (res.data.list.length > 0) {
            store.commit("setTeamList", res.data.list);
            // 如果公司下拉框参数为空，默认显示第一个
            setTimeout(() => {
                if (!selectCompany.value) {
                    res.data.list.forEach((v: any) => {
                        if (v.isMainTeam == 1) {
                            store.commit("selectTeam", v.teamId);
                        } else {
                            store.commit("selectTeam", res.data.list[0].teamId);
                        }
                    });
                }
                setTimeout(() => {
                    teamMsg(store.getters.mainTeamId).then((res: any) => {
                        store.commit("mainTeam", res.data);
                    });
                }, 100);
            }, 100);
        } else {
            store.commit("selectTeam", "");
            store.commit("mainTeam", "");
        }
    });
};
getTeamList();
const setCurrentMainTeam = (row: any) => {
    setMainTeams(row.teamId).then((res: any) => {
        ElMessage.success("设置成功");
        store.commit("selectTeam", row.teamId);
        store.commit("mainTeam", row);
        getTeamList();
    });
};
// 分页
const paginaClick = (val: number) => {
    queryData.pageNum = val;
    getTeamList();
};
const topages = (url: string) => {
    router.push(url);
};
</script>
<style lang="scss" scoped>
.teamTopTip {
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    .tipbox {
        margin-left: 10px;
        .tiptitle {
            font-size: 14px;
            // font-weight: bold;
            color: #252b3a;
        }
        .tipitem {
            margin-top: 10px;
            font-size: 14px;
            color: #252b3a;
            display: flex;
            align-items: center;
            // button {
            //     margin-top: -4px;
            // }
        }
    }
}
.teamBox {
    margin-top: 20px;
    background-color: #ffffff;
    border-radius: 5px;
    box-shadow: var(--el-box-shadow-light);
    .teamtop {
        border-bottom: 1px solid #eeeeee;
        padding: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .searchInput {
                width: 300px;
                margin-right: 10px;
            }
        }
    }
    .table {
        padding: 20px;
    }
}
</style>
